package in.muton.edushare.pages.user;

import in.muton.edushare.pages.univ.UnivPage;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.panels.paginator.EntityListing;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.vo.Universities;

public class UserUniv extends UserBase {

	public UserUniv(PageParameters params) {
		super(params);
		uL = null;
		if (ud.getUniv().getObject().size() == 1) {
			IModel<Universities> mU = ud.getUniv().getObject().get(0);
			WebPage p = new UnivPage(EntityPageParams.getParams(mU));
			throw new RestartResponseAtInterceptPageException(p);
		}
		add(new EntityListing<Universities>("thumbList", getUnivs(params), null));
	}

	private CayenneList<Universities> getUnivs(PageParameters params) {
		WebMarkupContainer wmc1, wmc2;
		String id = params.getString("1", "mine");
		add(wmc1 = new WebMarkupContainer("tab1"));
		add(wmc2 = new WebMarkupContainer("tab2"));
		wmc1.add(new BookmarkablePageLink<Void>("link", UserUniv.class,
				EntityPageParams.getParams(getModel(), "mine")));
		wmc2.add(new BookmarkablePageLink<Void>("link", UserUniv.class,
				EntityPageParams.getParams(getModel(), "all")));
		if (id.compareTo("mine") == 0) {
			uL = ud.getUniv();
			wmc1.add(new AttributeAppender("class", new Model<String>(
					"es-fbTabItemSelected"), " "));
			wmc2.add(new AttributeAppender("class", new Model<String>(
					"es-fbTabItem"), " "));

		} else if (id.compareTo("all") == 0) {
			uL = UnivDataHandler.getAllUnivs();
			wmc1.add(new AttributeAppender("class", new Model<String>(
					"es-fbTabItem"), " "));
			wmc2.add(new AttributeAppender("class", new Model<String>(
					"es-fbTabItemSelected"), " "));

		}
		return uL;
	}

	@Override
	public String getActivePage() {
		return "UserUniv";
	}

	@Override
	public String getPageTitle() {
		return getModel().getObject().getCompleteUserName()
				+ " | Universities | " + ApplicationConstants.APPLICATION_NAME;
	}

	private CayenneList<Universities> uL = null;

	@Override
	public String getUserRole() {
		return " : Universities";
	}
}
